package com.nineclock.system.controller;

import com.nineclock.api.system.SysCompanyControllerApi;
import com.nineclock.common.entity.Result;
import com.nineclock.system.dto.*;
import com.nineclock.system.service.SysCompanyService;
import com.nineclock.system.service.SysCompanyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/company")
public class SysCompanyController implements SysCompanyControllerApi {

    @Autowired
    private SysCompanyService companyService;
    @Autowired
    private SysCompanyUserService companyUserService;

    /**
     * 根据用户ID查询企业列表
     * @param userId
     * @return
     */
    @Override
    @GetMapping("/list/{userId}")
    public Result<List<SysCompanyDTO>> queryCompanyByUserId(@PathVariable("userId") Long userId) {

        List<SysCompanyDTO> companyDTOS = companyService.queryCompanyByUserId(userId);
        return Result.success(companyDTOS);
    }


    /**
     * 企业管理: 查询当前企业信息
     * @return
     */
    @Override
    @GetMapping
    public Result<SysCompanyDTO> queryCompany() {
        SysCompanyDTO sysCompanyDTO = companyService.queryCompany();
        return Result.success(sysCompanyDTO);
    }

    /**
     * 上传企业Logo
     */
    @Override
    @PostMapping("/uploadOSS")
    public Result uploadFile(MultipartFile file) {
        String result = companyService.uploadFile(file);
        return Result.success(result);
    }

    /**
     * 更新企业信息
     * @param companyDTO
     * @return
     */
    @Override
    @PutMapping
    public Result updateCompany(@RequestBody SysCompanyDTO companyDTO) {
        companyService.updateCompany(companyDTO);
        return Result.success();
    }

    /**
     * 获取企业当前主管理员
     * @return
     */
    @Override
    @GetMapping("/getCurrentAdmin")
    public Result<SysCompanyUserDTO> queryCurrentAdmin() {
        SysCompanyUserDTO companyUserDTO = companyUserService.queryCurrentAdmin();
        return Result.success(companyUserDTO);
    }

    /**
     * 更改主管理员信息
     * @param managerDTO
     * @return
     */
    @Override
    @PostMapping("/config/admin")
    public Result changeAdmin(@RequestBody SysChangeMainManagerDTO managerDTO) {
        companyService.changeAdmin(managerDTO);
        return Result.success();
    }

    /**
     * 根据企业的关键字, 模糊查询企业列表
     * @param keyword
     * @return
     */
    @Override
    @GetMapping("/list")
    public Result<List<SysCompanyDTO>> queryByKeyword(String keyword) {
        List<SysCompanyDTO> companyDTOList = companyService.queryByKeyword(keyword);
        return Result.success(companyDTOList);
    }

    /**
     * 申请加入企业
     * @param companyUserDTO
     * @return
     */
    @Override
    @PostMapping("/applyJoinCompany")
    public Result applyJoinCompany(@RequestBody SysApplyJoinCompanyUserDTO companyUserDTO) {
        companyService.applyJoinCompany(companyUserDTO);
        return Result.success();
    }

    /**
     * 审核加入企业
     * @param allowedDTO
     * @return
     */
    @Override
    @PostMapping("/allowedJoinCompany")
    public Result allowedJoinCompany(SysAllowedJoinCompanyUserDTO allowedDTO) {

        companyService.allowedJoinCompany(allowedDTO);
        return Result.success();
    }
}